/**
 * Copyright 2011 Micheal Swiggs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.bitcoin.discovery;

import com.google.bitcoin.core.NetworkParameters;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

/**
 * SeedPeers stores a pre-determined list of Bitcoin node addresses. These nodes are selected based on being
 * active on the network for a long period of time. The intention is to be a last resort way of finding a connection
 * to the network, in case IRC and DNS fail. The list comes from the Bitcoin C++ source code.
 */
public class SeedPeers implements PeerDiscovery {
    private NetworkParameters params;
    private int pnseedIndex;

    public SeedPeers(NetworkParameters params) {
        this.params = params;
    }

    /**
     * Acts as an iterator, returning the address of each node in the list sequentially.
     * Once all the list has been iterated, null will be returned for each subsequent query.
     *
     * @return InetSocketAddress - The address/port of the next node.
     * @throws PeerDiscoveryException
     */
    public InetSocketAddress getPeer() throws PeerDiscoveryException {
        try {
            return nextPeer();
        } catch (UnknownHostException e) {
            throw new PeerDiscoveryException(e);
        }
    }

    private InetSocketAddress nextPeer() throws UnknownHostException {
        if (pnseedIndex >= seedAddrs.length) return null;
        return new InetSocketAddress(convertAddress(seedAddrs[pnseedIndex++]),
                params.getPort());
    }

    /**
     * Returns an array containing all the Bitcoin nodes within the list.
     */
    public InetSocketAddress[] getPeers(long timeoutValue, TimeUnit timeoutUnit) throws PeerDiscoveryException {
        try {
            return allPeers();
        } catch (UnknownHostException e) {
            throw new PeerDiscoveryException(e);
        }
    }

    private InetSocketAddress[] allPeers() throws UnknownHostException {
        InetSocketAddress[] addresses = new InetSocketAddress[seedAddrs.length];
        for (int i = 0; i < seedAddrs.length; ++i) {
            addresses[i] = new InetSocketAddress(convertAddress(seedAddrs[i]), params.getPort());
        }
        return addresses;
    }

    private InetAddress convertAddress(int seed) throws UnknownHostException {
        byte[] v4addr = new byte[4];
        v4addr[0] = (byte) (0xFF & (seed));
        v4addr[1] = (byte) (0xFF & (seed >> 8));
        v4addr[2] = (byte) (0xFF & (seed >> 16));
        v4addr[3] = (byte) (0xFF & (seed >> 24));
        return InetAddress.getByAddress(v4addr);
    }

     
	 
	public static int[] seedAddrs =
            {
                    0x92114064, 0xca86a665, 0x7189a665, 0xb49c6265, 0xc15ebb6a, 0x7e5fbb6a, 0xcdf6d46b, 0xce5cda6b,
					0xb596dd6b, 0x31d9de6b, 0xd55cb76c, 0x4f42ed6c, 0x45f91b6c, 0xed2c236c, 0xb5f6a56d, 0xfb5e506d,
					0x729b886e, 0x4ee48f6e, 0xa2976a71, 0xf2c87471, 0x6b997a71, 0xfb450d71, 0xf5bea271, 0x4e10210b,
					0x50a7d972, 0x55ad5672, 0xa5d89473, 0xddd8c273, 0x4a744476, 0x902c5177, 0x45c79278, 0x11ea0a79,
					0x4ee3db79, 0xaa02ec79, 0xfc2e967a, 0x6a90e07a, 0x06ca8b7b, 0x3d88107b, 0xd445617b, 0xc631787c,
					0xc899957c, 0xc7a2a97c, 0xb24a6d7d, 0xb84d787d, 0x55f2a77d, 0x0e3d6f80, 0xc4e0fd80, 0x5a0f9f83,
					0x7e0b1186, 0xfa081186, 0xa33ca78e, 0xad454c90, 0xb7af369b, 0x5eaf369b, 0xa806caa2, 0xec907ea4,
					0x5f974ea6, 0x6af1faac, 0xf55504ac, 0x3c5e1cad, 0x246f2cad, 0xe38334ae, 0x12ecb0af, 0xb8a002b0,
					0xd0543db0, 0x8af873b2, 0x83da77b2, 0xfd7d7cb2, 0xea600fb2, 0x6127ddb2, 0x953ddfb2, 0x03cd1ab2,
					0x99da20b2, 0x5ec831b2, 0x33ad4fb2, 0xafed52b2, 0x2c3d09b2, 0xbb299cb7, 0x488f47b7, 0x79b000b8,
					0x7459b9b8, 0xd0f738b8, 0x2aee48b8, 0x4adf4bb8, 0xff1072bc, 0x1d1b8ebc, 0xf49c9abc, 0x4043bfbc,
					0xfe11eebc, 0x6b8bfcbc, 0x720d20bc, 0xc92728bc, 0x6ecd2fbd, 0x48e864be, 0xe6a255c1, 0x576369c2,
					0xedd697c3, 0x087990c6, 0x0c6dc7c6, 0x3347c7c6, 0xd250c7c6, 0xd66c5ac6, 0x3e6d5ac6, 0xb53907c8,
					0xa73718c9, 0x436cbfca, 0x0fe4dbcb, 0xc2714ccb, 0x71690acc, 0x11fe60cf, 0xc39768d0, 0x96176fd4,
					0xa571dcd4, 0xeae6ffd4, 0x5ee9ffd4, 0x39d09ed5, 0x1bbba8d5, 0xcb412fd5, 0x917857d5, 0xdd1388d9,
					0x5e5e91d9, 0x0a781dd9, 0x44a956d9, 0x231b60d9, 0x7e6ac9da, 0x0817c9da, 0x22d850da, 0x7a6b54da,
					0x312ef4dc, 0x10cb51de, 0x46e1ffdf, 0x47e6ffdf, 0x41331017, 0x5b4b6c18, 0x5d927018, 0x037b9918,
					0x415ef718, 0x67dd0618, 0xedec4e18, 0x91d55418, 0xa6c2bc1b, 0x71c35d02, 0x3749ae1f, 0x6399c81f,
					0x3800dc1f, 0x18691f1f, 0x53166e25, 0x90e4bc25, 0xaa2fd025, 0xf5110425, 0xbb990925, 0x4536732e,
					0x6747732e, 0xea4b732e, 0x32b0952e, 0xf27f962e, 0xbf12bc2e, 0x14d6ee2e, 0xdd49f22e, 0xd591fb31,
					0x582d8332, 0x16d78a32, 0x99a31332, 0x6f731732, 0x66382b32, 0x6c6d4832, 0x573c5132, 0xce30a405,
					0x8c4f2705, 0x115a2705, 0x826df436, 0x2785f436, 0xa2ec643a, 0x2f580b3a, 0x07610b3a, 0xa294d83a,
					0xc4f91a3a, 0xe926653b, 0x6126653b, 0xe0cba73b, 0xe4274e3b, 0x0800bc3c, 0x6880ac3d, 0x73e39d3e,
					0x57c79e3e, 0x6840c43e, 0xc763d43e, 0x6e92f93e, 0x8bb54b3e, 0x02a18e3f, 0x08a18e3f, 0xa635b740,
					0x1faebc40, 0x7725ed40, 0x3ebcfb40, 0x7399a741, 0xaca82241, 0xd3b9a242, 0xe15db742, 0xeafa2d42,
					0x94048d43, 0xd6078d43, 0xda80b843, 0xbb90d943, 0xe2a26c44, 0x84de7444, 0xe0a64144, 0xb20a5344,
					0xad726144, 0x88d36144, 0x13cca445, 0x27f8a545, 0xc3e41145, 0x8ffff445, 0x55dcf845, 0x3ca82345,
					0x81405045, 0x7e980945, 0x68847046, 0x0f2d4c46, 0xba197a47, 0x508bc147, 0x52d23847, 0x7e143b47,
					0x370e5d47, 0x3a52b148, 0xf0afc848, 0x608dea48, 0xab1e0948, 0x7d8e684a, 0x47aad04a, 0xe4d13f4a,
					0xbfa3414a, 0xe1384c4a, 0xea77784b, 0x1595854b, 0x82bba14b, 0x21ff6e4c, 0x496d7b4c, 0x567f1e4c,
					0xd5585e4c, 0x2b38a84d, 0x3fc7bf4d, 0xc1366c4e, 0x1bdcf74e, 0x07228b4f, 0x1545994f, 0x3cdbf14f,
					0xc888284f, 0x2765b150, 0xce62d450, 0x2349dc50, 0x35abee50, 0x00d6be51, 0x5ab11e51, 0xb22a3e51,
					0x1f060c52, 0x3e0d8b52, 0x3aa4d952, 0x91c0e052, 0x8684e652, 0xdaf41752, 0x17c1ea52, 0x5d1b2452,
					0xc4229553, 0x73491753, 0x4d962053, 0x32436a54, 0x190d6f54, 0x6fd68754, 0xd5223054, 0x6af93a54,
					0xb6ea0a55, 0x5ea08b55, 0x1189b055, 0x0f96d855, 0x84b2de55, 0x59c8e455, 0x9373ee55, 0xe452f055,
					0x19450455, 0xfc8f9256, 0x3987a656, 0x261b2056, 0x842d3d56, 0x3c8a7057, 0xce0bb757, 0xa964c257,
					0xfefa6257, 0x1963a158, 0xdfb56659, 0x7dd58e59, 0xb387cc59, 0xcdd70759, 0xf1efbc5a, 0xc773d75a,
					0x29a9735b, 0xc5ba755b, 0x1e907b5b, 0x2e0e595b, 0x96a05a5b, 0xa800765c, 0x9452ea5c, 0x05b0ef5c,
					0x8fb2f05c, 0x25f51b5c, 0x86c1275c, 0x435f515c, 0x48587e5d, 0x2b41d45d, 0xa6004b5d, 0x5cb0575d,
					0x4684605d, 0xa741715e, 0x055eae5e, 0x3437bc5e, 0xc0c9d05e, 0x101ce65e, 0x92bdfd5e, 0x78fb495e,
					0xa10f695f, 0x8a9c6e5f, 0xcfa0855f, 0xebca855f, 0x1d09105f, 0xcb26ae5f, 0x5aedf35f, 0xe7c4205f,
					0x7de1215f, 0x55f8395f, 0xf370f860, 0xfb822b60, 0xaa8f2c60, 0x027d5161, 0x22d25261, 0x442b5661,
					0xceae5961, 0xa3041462, 0xf01acc62, 0xe719e862, 0x44a71c62, 0x48046663, 0xcd3d8763, 0x4e59a263
            };
    
    public void shutdown() {
    }
}
